define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pro/breed/index' + location.search,
                    add_url: 'pro/breed/add',
                    edit_url: 'pro/breed/edit',
                    del_url: 'pro/breed/del',
                    multi_url: 'pro/breed/multi',
                    import_url: 'pro/breed/import',
                    table: 'pro_breed',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'factory_id', title: __('Factory_id')},
                        {field: 'admin_id', title: __('Admin_id')},
                        {field: 'group_id', title: __('Group_id')},
                        {field: 'batch_id', title: __('Batch_id')},

                        // {field: 'user', title: "提交人"},
                        // {field: 'group', title: __('Group_id')},
                        // {field: 'pici', title: __('Batch_id')},

                        {field: 'num', title: __('Num')},
                        {field: 'light_num', title: __('Light_num')},
                        {field: 'rate', title: __('Rate'), operate:'BETWEEN', formatter: Controller.api.formatter.rate },
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'newdate', title: __('Newdate'), formatter: Table.api.formatter.datetime, datetimeFormat: 'YYYY-MM-DD', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, datetimeFormat: 'YYYY-MM-DD', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'updatetime', title: __('Updatetime'), formatter: Table.api.formatter.datetime, datetimeFormat: 'YYYY-MM-DD', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'status', title: __('Status'), searchList: {"待审核":__('待审核'),"已通过":__('已通过'),"已驳回":__('已驳回')}, formatter: Table.api.formatter.status},
                        {field: 'apply_user', title: __('Apply_user')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'pro/breed/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'pro/breed/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'pro/breed/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            $("#c-batch_id").data("params", function(){
                return {custom: {group_id: $("#c-group_id").val()}};
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                //  TODO.DEMO
                rate: function (value, row, index) {
                    return row.rate + '%' ;
                },
                thumb: function (value, row, index) {
                    var html = '';

                    html = '<a href="' + row.img + '" target="_blank"><img src="' + row.img +'" alt="" style="max-height:60px;max-width:120px"></a>';

                    return '<div style="width:120px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + html + '</div>';
                },
                //  ##### 显示切换隐藏不好使
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('点击展开子项目') + '" data-shown="false" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
                        + (row.ismain == 1 || row.ismain == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub" ><i class="fa fa-sitemap"></i></a>';
                },
                //  列表样式
                cck: function (value, row, index) {
                    var custom = {1: 'success', 2: 'gray', 3: 'danger', 4: 'info'};
                    if (typeof this.custom !== 'undefined') {
                        custom = $.extend(custom, this.custom);
                    }
                    this.custom = custom;
                    //this.icon = 'fa fa-circle';
                    this.icon = '';
                    return Table.api.formatter.normal.call(this, value, row, index);
                },
                contentc: function (value, row, index) {
                    var width = this.width != undefined ? this.width : 100;
                    var value = value ? value : "-";
                    return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + "px;'>" + value + "</div>";
                },
                reurl1: function (value, row, index) {
                    // ######
                    var url = "/UuOBJcrnPd.php/joinactive/index?xf=1&active_id=" + value.id ;
                    return url;
                }
            }
        }
    };
    return Controller;
});
